<?php
include "./inc/PAGE.php";
$username = $_POST['username'];
$password = $_POST['password'];
$s = new SESSION();
$t = new TOOLBOX();
$db = new DB(DBURL, DBUSER, DBPASS, DBNAME);

if(strlen($username)>0 && strlen($password)>0) {
    $username = $t->cleaner($_POST['username']);
    $password = $t->cleaner($_POST['password']);
    $password = md5($password);

    $sql = "SELECT COUNT(*) as loginOk, type, name
        FROM users WHERE
        BINARY username='".$username."' AND
            BINARY password='".$password."'";

    $login = $db->gettoprow($sql);

    if($login['loginOk']==0) {
        $error = "*Λάθος κωδικός χρήστη ή συνθηματικό!";
        $s->unsetvar("loginOk");
        header("Location: login.php?error=".$error);
    }else {
        
        $s->setvar("loginOk",true);
        $s->setvar("username",$username);
        $s->setvar("type",$login['type']);
        $s->setvar("name",$login['name']);
        header("Location: ./ze_library.php");
    }


    $t->debug($val);

    exit();


}else {
    $error = "* Πρέπει να συμπληρώσετε όνομα χρήστη και τον κωδικό!<br/>";
    $s->unsetvar("loginOk");
    header("Location: login.php?error=".$error);
}
echo '<br/>';
echo "#".$username."#".$password."#<br/>";
?>
